package pl.shenlon.ga;

import pl.shenlon.poker.game.GameState;

public class SimpleSpecimen extends Specimen {

	public static SimpleSpecimen[] crossover(double crossoverRate,
			SimpleSpecimen parentA, SimpleSpecimen parentB) {

		SimpleSpecimen[] children = new SimpleSpecimen[]{
				new SimpleSpecimen(parentA),
				new SimpleSpecimen(parentB),};
		if (rnd.nextDouble() < crossoverRate) {
			BehaviourPattern parentABehaviourPattern = parentA.genom
					.getBehaviourPattern(GameState.ANY), parentBBehaviourPattern = parentB.genom
					.getBehaviourPattern(GameState.ANY), childABheaviourPattern = children[0]
					.getGenom().getBehaviourPattern(GameState.ANY), childBBehaviourPattern = children[1]
					.getGenom().getBehaviourPattern(GameState.ANY);
			for (int i = 0; i < parentABehaviourPattern.getLength(); i++) {
				int valueA = 0, valueB = 0;
				valueA = (int) (0.8 * parentABehaviourPattern.getValue(i) + 0.2 * parentBBehaviourPattern
						.getValue(i));
				valueB = (int) (0.2 * parentABehaviourPattern.getValue(i) + 0.8 * parentBBehaviourPattern
						.getValue(i));
				childABheaviourPattern.setValue(i, valueA);
				childBBehaviourPattern.setValue(i, valueB);
			}
		}
		return children;
	}
	public SimpleSpecimen() {
		super(new SimpleChromosome());
		BehaviourPattern pattern = genom
				.getBehaviourPattern(GameState.POSSIBLE_GAME_STATES.get(0));
		int genomLength = pattern.getLength(), max = genomLength * 20;
		for (int i = 0; i < genomLength; i++) {
			int value = rnd.nextInt(max + 1);
			max -= value;
			pattern.setValue(i, value);
		}

	}

	public SimpleSpecimen(BehaviourPattern pattern) {
		this(new SimpleChromosome(pattern));
	}

	public SimpleSpecimen(int[] genom) {
		super(new SimpleChromosome(genom));
	}

	public SimpleSpecimen(SimpleChromosome genom) {
		super(genom);
	}

	public SimpleSpecimen(SimpleSpecimen specimen) {
		this((SimpleChromosome) specimen.genom);
	}

	@Override
	public Specimen copy() {
		return new SimpleSpecimen(this);
	}
	@Override
	public Specimen[] crossover(double crossoverRate, Specimen other) {
		if (other instanceof SimpleSpecimen == false) {
			throw new IllegalArgumentException(
					"You can't crossover two specimens of different types.");
		}
		return SimpleSpecimen.crossover(crossoverRate, this,
				(SimpleSpecimen) other);
	}

}